﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Net_SCADA;

namespace WZXC_GBL_QUERY
{
    public partial class Form_QueryAlarmByDay : Form
    {
        string[] his_AlarmTitle=new string[5]{ "序号","警报名称", "警报等级","发生时间" ,"消除时间"};
        private Size ScreenSize;
        bool isLoadInterface;
        DateTime QueryDate=DateTime.Now;

        DateTime queryStarttime;
        DateTime queryEndtime;
        bool isQueryByDay;
        string BatchSerial;

        public Form_QueryAlarmByDay(bool queryByDay, string batchserial, DateTime dtStart, DateTime dtEnd)
        {
            InitializeComponent();
            queryStarttime = dtStart;
            queryEndtime = dtEnd;
            isQueryByDay = queryByDay;
            BatchSerial = batchserial;
        }
        public Form_QueryAlarmByDay(DateTime dt)
        {
            InitializeComponent();
            QueryDate = dt;
        }

        private void Form_QueryAlarmByDay_Load(object sender , EventArgs e)
        {
            isLoadInterface = true;
            dtp_Day.Value = QueryDate;
            ScreenSize = Screen.PrimaryScreen.WorkingArea.Size;
            Show();
            setInterface();
            isLoadInterface = false;
            showAlarmByDay(QueryDate);
        }

        private void setInterface()
        {

            pnl_AlarmQuery.Size  = new Size(ScreenSize.Width , ScreenSize.Height - 60);
            btn_Query.Location = new Point(ScreenSize.Width - btn_Query.Width - 2 , 1);
            dtp_Day.Location = new Point(ScreenSize.Width - btn_Query.Width - dtp_Day.Width - 5 , 1);

        }

        private void LoadAlarmInterface()
        {
            lst_Alarm.Clear();
            lst_Alarm.Location = new Point(2 , 30);
            lst_Alarm.Size = new Size(pnl_AlarmQuery.Width - 4 , pnl_AlarmQuery.Height - 40);
            lst_Alarm.Columns.Add(his_AlarmTitle[0] , 100 , HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[1] , lst_Alarm.Width - 622 , HorizontalAlignment.Center);
            lst_Alarm.Columns.Add(his_AlarmTitle[2] , 100 , HorizontalAlignment.Left);
            lst_Alarm.Columns.Add(his_AlarmTitle[3] , 200 , HorizontalAlignment.Left); 
            lst_Alarm.Columns.Add(his_AlarmTitle[4] , 200 , HorizontalAlignment.Left);
        }

        
        private void showAlarmByDay(DateTime datetime)
        {
            LoadAlarmInterface();
            DataTable dt = DBOperate.his_GetAlarmValues(datetime, datetime.AddDays(1).AddSeconds(-1));

            if (dt == null || dt.Rows.Count == 0)
            {
                lbl_AlarmQueryTitle.Text = datetime.ToString("yyyy年MM月dd日") + " 没有警报记录";
                return;
            }
            lbl_AlarmQueryTitle.Text = datetime.ToString("yyyy年MM月dd日") + " 警报记录";
            for (int i = dt.Rows.Count - 1 ; i >= 0 ; i--)
            {
                if (dt.Rows[i]["AlarmActive"].ToString() == "0")
                {
                    lst_Alarm.Items.Add(new ListViewItem(new string[] { (lst_Alarm.Items.Count + 1).ToString() , dt.Rows[i]["AlarmName"].ToString() , dt.Rows[i]["AlarmLevel"].ToString() , "" , dt.Rows[i]["SaveTime"].ToString() }));//最后是报警等级，最后修改
                }
                else
                {
                    //1这里加载数据，对数据进行重组
                    int lastindex = -10;
                    for (int k = lst_Alarm.Items.Count - 1 ; k >= 0 ; k--)
                    {
                        if (dt.Rows[i]["AlarmName"].ToString() == lst_Alarm.Items[k].SubItems[1].Text && lst_Alarm.Items[k].SubItems[3].Text == "")
                        {
                            lst_Alarm.Items[k].SubItems[3].Text = dt.Rows[i]["SaveTime"].ToString();
                            lastindex = k;
                            break;
                        }
                    }
                    if (lastindex == -10)
                    {
                        lst_Alarm.Items.Add(new ListViewItem(new string[] { (lst_Alarm.Items.Count + 1).ToString() , dt.Rows[i]["AlarmName"].ToString() , dt.Rows[i]["AlarmLevel"].ToString() , dt.Rows[i]["SaveTime"].ToString() , "" }));
                    }
                }
            }
        }

        private void btn_Query_Click(object sender , EventArgs e)
        {
            if (isLoadInterface) return;
            showAlarmByDay(dtp_Day.Value);
        }
    }
}
